home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jparseargs.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  2.4 KB  |  67 lines

  1. # jparseargs.tcl - parse procedure options in -name value style
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non¡profit, noncommercial use.
  5. ######################################################################
  6.  
  7. ### TO DO
  8. ###   j:parse_args -boolean (and maybe -position?)
  9.  
  10. ######################################################################
  11. # j:parse_args arglist - parse arglist in parent procedure
  12. #   arglist is a list of option names (without leading "-");
  13. # this proc puts their values (if any) into variables (named after
  14. #   the option name) in d parent procedure
  15. # any element of arglist can also be a list consisting of an option
  16. #   name and a default value.
  17. ######################################################################
  18.  
  19. proc j:parse_args { arglist } {
  20.   upvar args args
  21.   
  22.   foreach pair $arglist {
  23.     set option [lindex $pair 0]
  24.     set default [lindex $pair 1]        ;# will be null if not supplied
  25.     set index [lsearch -exact $args "-$option"]
  26.     if {$index != -1} {
  27.       set index1 [expr {$index + 1}]
  28.       set value [lindex $args $index1]
  29.       uplevel 1 [list set $option $value]    ;# caller's variable "$option"
  30.       set args [lreplace $args $index $index1]
  31.     } else {
  32.       uplevel 1 [list set $option $default]    ;# caller's variable "$option"
  33.     }
  34.   }
  35.   return 0
  36. }
  37.  
  38. ######################################################################
  39. # j:parse_argv arglist - parse application's argv (and update argc)
  40. #   arglist is a list of option names (without leading "-");
  41. # this proc puts their values (if any) into variables (named after
  42. #   the option name) in the parent procedure
  43. # any element of arglist can also be a list consisting of an option
  44. #   name and a default value.
  45. ######################################################################
  46.  
  47. proc j:parse_argv { arglist } {
  48.   global argv argc
  49.   
  50.   foreach pair $arglist {
  51.     set option [lindex $pair 0]
  52.     set default [lindex $pair 1]        ;# will be null if not supplied
  53.     set index [lsearch -exact $argv "-$option"]
  54.     if {$index != -1} {
  55.       set index1 [expr {$index + 1}]
  56.       set value [lindex $argv $index1]
  57.       uplevel 1 [list set $option $value]    ;# caller's variable "$option"
  58.       set argv [lreplace $argv $index $index1]
  59.     } else {
  60.       uplevel 1 [list set $option $default]    ;# caller's variable "$option"
  61.     }
  62.   }
  63.   set argc [llength $argv]
  64.   return 0
  65. }
  66.